package com.bc.commonentity.common;

import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.enums.HttpReturnRnums;

/**
 * Created by lianzerong on 2018/8/22.
 */
public class MarvelException extends RuntimeException {

    /** 错误枚举 */
    private HttpReturnRnums error;
    /** 错误描述 */
    private String errorMsg;

    public MarvelException() {
        super();
    }

    public MarvelException(HttpReturnRnums error) {
        super(error.desc());
        this.error = error;
    }

    /**
     * 需要展示特殊错误码描述
     *
     * @param error
     * @param errorMsg
     */
    public MarvelException(HttpReturnRnums error, String errorMsg) {
        super(error.desc());
        this.errorMsg = errorMsg;
        this.error = error;
    }

    /**
     * 获取错误码
     *
     * @return
     */
    public int getErrorCode() {
        if (this.getError() != null) {
            return this.getError().value();
        }
        return 1;
    }

    /**
     * 获取错误描述
     *
     * @return
     */
    public String getErrorMsg() {
        if (!StringUtils.isEmpty(errorMsg)) {
            return errorMsg;
        }
        if (this.getError() != null) {
            return this.getError().desc();
        }
        return null;
    }

    /**
     * @return the error
     */
    public HttpReturnRnums getError() {
        return error;
    }

    /**
     * @param error
     *            the error to set
     */
    public void setError(HttpReturnRnums error) {
        this.error = error;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
}
